package shuai7boy.vip.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.hadoop.conf.Configuration;

import shuai7boy.vip.common.GlobalConstants;


public class JdbcManager {

	/**
	 * 根据配置获取关系型数据库
	 * @param conf
	 * @param flag
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection(Configuration conf, String flag) throws SQLException {
		String driverStr = String.format(GlobalConstants.JDBC_DRIVER, flag);
		String urlStr = String.format(GlobalConstants.JDBC_URL, flag);
		String usernameStr = String.format(GlobalConstants.JDBC_USERNAME, flag);
		String passwordStr = String.format(GlobalConstants.JDBC_PASSWORD, flag);

		String driverClass = conf.get(driverStr);
		String url = conf.get(urlStr);
		String username = conf.get(usernameStr);
		String password = conf.get(passwordStr);
		try {
			Class.forName(driverClass);
		} catch (ClassNotFoundException e) {
			// nothing
		}
		return DriverManager.getConnection(url, username, password);
	}
}
